package com.example.administrator.demob;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private InOnDelListener listener;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList, InOnDelListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.stu_item, viewGroup, false);

            StuViewHolder stuViewHolder = new StuViewHolder();
            stuViewHolder.txtNo = view.findViewById(R.id.txt_no);
            stuViewHolder.txtName = view.findViewById(R.id.txt_name);
            stuViewHolder.txtAge = view.findViewById(R.id.txt_age);
            stuViewHolder.txtSex = view.findViewById(R.id.txt_sex);
            stuViewHolder.ivDel = view.findViewById(R.id.iv_del);

            view.setTag(stuViewHolder);
        }

        StuViewHolder viewHolder = (StuViewHolder) view.getTag();
        final Student s = studentArrayList.get(i);

        viewHolder.txtNo.setText(s.getStuNo());
        viewHolder.txtName.setText(s.getName());
        viewHolder.txtAge.setText(String.valueOf(s.getAge()));
        viewHolder.txtSex.setText(s.getSex());

        viewHolder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(s);
            }
        });

        return view;
    }
}
